package organization

import (
	"context"
	"git.mycaigou.com/gfyx/micro-gfyx-user-service/infrastructure/repository/organization"
	"git.mycaigou.com/gfyx/micro-gfyx-user-service/infrastructure/repository/remote/modeling_paltform_remote"
	"git.myscrm.cn/golang/common/uuid"
	"git.myscrm.cn/golang/stark/v4"
	"time"
)

const (
	TENANT_INFO_CACHE_KEY = "gfyx:organization_service:tenant_list:"
)

type OrganizationService struct {
	commonOrganizationDataRepo organization.CommonOrganizationRepoIface
}

func NewOrganizationService(commonOrganizationDataRepo organization.CommonOrganizationRepoIface) OrganizationServiceIface {
	return &OrganizationService{
		commonOrganizationDataRepo: commonOrganizationDataRepo,
	}
}

func (u *OrganizationService) BatchSaveOrganization(ctx context.Context, tenantCode string, organizationList []*modeling_platfrom_remote.OrganizationData) (bool, error) {
	condition := map[string]interface{}{}
	condition["tenant_code"] = tenantCode
	for _, info := range organizationList {
		if info.BUGUID == "" {
			stark.Logger.Warn(ctx, "过滤组织id为空的组织信息，租户code：", tenantCode, ",组织信息: %+v", info)
			continue
		}
		condition["bu_uid"] = info.BUGUID
		commonOrganization, err := u.commonOrganizationDataRepo.GetOrganizationInfo(ctx, condition)
		if err != nil {
			return false, err
		}
		if commonOrganization != nil && commonOrganization.Uid != "" {
			//update
			updateData := map[string]interface{}{
				"bu_code":       info.BuCode,
				"parent_bu_uid": info.ParentGUID,
				"bu_type":       info.BUType,
				"level":         info.Level,
				"bu_name":       info.BUName,
				"bu_full_name":  info.BuFullName,
				"company_uid":   info.CompanyGUID,
				"is_disabled":   info.IsDisabled,
				"modified_time": time.Now(),
			}
			updateCondition := map[string]interface{}{
				"uid": commonOrganization.Uid,
			}

			flag, err := u.commonOrganizationDataRepo.Update(ctx, updateData, updateCondition)
			if err != nil {
				stark.Logger.Warn(ctx, "组织更新出错，租户code："+tenantCode+",组织id："+info.BUGUID)
				return false, err
			}
			if flag == false {
				stark.Logger.Warn(ctx, "组织更新失败，租户code："+tenantCode+",组织id："+info.BUGUID)
				return false, nil
			}
		} else {
			commonOrganization := &organization.CommonOrganization{}
			commonOrganization.Uid = uuid.GetUUID()
			commonOrganization.BuUid = info.BUGUID
			commonOrganization.TenantCode = tenantCode
			commonOrganization.BuCode = info.BuCode
			commonOrganization.ParentBuUid = info.ParentGUID
			commonOrganization.BuType = info.BUType
			commonOrganization.Level = info.Level
			commonOrganization.BuName = info.BUName
			commonOrganization.BuFullName = info.BuFullName
			commonOrganization.CompanyUid = info.CompanyGUID
			commonOrganization.IsDisabled = 0
			if info.IsDisabled == true {
				commonOrganization.IsDisabled = 1
			}
			commonOrganization.CreatedTime = time.Now()
			flag, err := u.commonOrganizationDataRepo.Insert(ctx, commonOrganization)
			if err != nil {
				stark.Logger.Warn(ctx, "组织写入出错，租户code："+tenantCode+",组织id："+info.BUGUID)
				return false, err
			}
			if flag == false {
				stark.Logger.Warn(ctx, "组织写入失败，租户code："+tenantCode+",组织id："+info.BUGUID)
				return false, nil
			}
		}
	}
	return true, nil
}

//func (u *OrganizationService) IncrSyncOrganization(ctx context.Context, tenantCode, operation string, userList []mp_remote.Organization) (bool, error) {
//	if operation == "I" || operation == "U" {
//		flag, err := u.BatchSaveOrganization(ctx, tenantCode, userList)
//		if err != nil {
//			return false, err
//		}
//		if flag == false {
//			return false, errors.New("组织保存失败，租户code：" + tenantCode)
//		}
//	} else if operation == "D" {
//		condition := map[string]interface{}{}
//		for _, info := range userList {
//			condition["tenant_user_uid"] = info.OrganizationId
//			condition["tenant_code"] = tenantCode
//			u.commonOrganizationDataRepo.Delete(ctx, condition)
//		}
//	}
//
//	return false, nil
//}
